package com.xiaoxing.zhinenghuacangku_linyi.mvp.presenter;

import android.app.Application;
import android.content.Context;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;
import com.xiaoxing.zhinenghuacangku_linyi.mvp.contract.LinYi_OutOfTheLibraryContract;
import com.xiaoxing.zhinenghuacangku_linyi.mvp.model.entity.OutOfTheLibraryList;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


@ActivityScope
public class LinYi_OutOfTheLibraryPresenter extends BasePresenter
        <LinYi_OutOfTheLibraryContract.Model, LinYi_OutOfTheLibraryContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public LinYi_OutOfTheLibraryPresenter(LinYi_OutOfTheLibraryContract.Model model, LinYi_OutOfTheLibraryContract.View rootView) {
        super(model, rootView);
    }

    public void searchOutStorageListAutoStore(Context context, String userPhone, String uid) {

        mModel.searchOutStorageListAutoStore(userPhone, uid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String searchOutStorageListAutoStore) {
                        try {
                            String result = searchOutStorageListAutoStore.substring(75, searchOutStorageListAutoStore.toCharArray().length - 9);
                            if (result.equals("[]") || result.equals("Error")) {
                                ArmsUtils.makeText(context, "没有搜索到相关数据");
                                mRootView.searchNoData();
                            } else {
                                List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                                mRootView.searchOutStorageListAutoStoreSuccess(outOfTheLibraryLists);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void returnMaterialAutoStore(Context context, String out_storageList_id, String mid, String uid) {

        mModel.returnMaterialAutoStore(out_storageList_id, mid, uid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String returnMaterialAutoStore) {
                        try {
                            String result = returnMaterialAutoStore.substring(75, returnMaterialAutoStore.toCharArray().length - 9);
                            if (result.equals("[]")) {
                                ArmsUtils.makeText(context, "没有搜索到相关数据");
                            } else {
                                //                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                                mRootView.returnMaterialAutoStoreSuccess(returnMaterialAutoStore);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void callAutoMachineGetMaterial(Context context, String out_storagelist_batchid, String uid) {

        mModel.callAutoMachineGetMaterial(out_storagelist_batchid, uid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String callAutoMachineGetMaterial) {
                        String result = callAutoMachineGetMaterial.substring(75, callAutoMachineGetMaterial.toCharArray().length - 9);
                        if (result.equals("[]")) {
//                            ArmsUtils.makeText(context, "没有搜索到相关数据");
                        } else {
//                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                            mRootView.callAutoMachineGetMaterialSuccess(callAutoMachineGetMaterial);
                        }
                    }
                });
    }

    public void finishErrorTask(Context context, String out_storagelist_batchid) {

        mModel.finishErrorTask(out_storagelist_batchid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String finishErrorTask) {
                        try {
                            String result = finishErrorTask.substring(75, finishErrorTask.toCharArray().length - 9);
                            if (result.equals("[]")) {
                                //                            ArmsUtils.makeText(context, "没有搜索到相关数据");
                            } else {
                                //                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                                mRootView.finishErrorTaskSuccess(finishErrorTask);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void outStoreForAndroidAuto(Context context, String uid, String mid, String out_storageList_id, String bufferId, String taskId) {

        mModel.outStoreForAndroidAuto(uid, mid, out_storageList_id, bufferId, taskId).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String outStoreForAndroidAuto) {
                        String result = outStoreForAndroidAuto.substring(72, outStoreForAndroidAuto.toCharArray().length - 6);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "没有搜索到相关数据");
                        } else {
//                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                            mRootView.outStoreForAndroidAutoSuccess(result);
                        }
                    }
                });
    }


    public void returnMaterialAllAutoStore(Context context, String uid, String mid, String out_storageList_id, String bufferId, String taskId) {

        mModel.returnMaterialAllAutoStore(uid, mid, out_storageList_id, bufferId, taskId).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String returnMaterialAllAutoStore) {
                        String result = returnMaterialAllAutoStore.substring(72, returnMaterialAllAutoStore.toCharArray().length - 6);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "没有搜索到相关数据");
                        } else {
//                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                            mRootView.returnMaterialAllAutoStoreSuccess(result);
                        }
                    }
                });
    }


    public void UpdateOutStorageListBackStatus(Context context, String out_storageList_id, String backStatus) {

        mModel.UpdateOutStorageListBackStatus(out_storageList_id, backStatus).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String returnMaterialAutoStore) {
                        String result = returnMaterialAutoStore.substring(75, returnMaterialAutoStore.toCharArray().length - 9);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "");
                        } else {
//                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                            mRootView.UpdateOutStorageListBackStatusSuccess(returnMaterialAutoStore);
                        }
                    }
                });
    }

    public void UpdateOutStorageListScanBufferStatus(Context context, String out_storageList_id, String bufferStatus, String bufferId) {

        mModel.UpdateOutStorageListScanBufferStatus(out_storageList_id, bufferStatus, bufferId).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String returnMaterialAutoStore) {
                        String result = returnMaterialAutoStore.substring(75, returnMaterialAutoStore.toCharArray().length - 9);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "");
                        } else {
//                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                            mRootView.UpdateOutStorageListScanBufferStatusSuccess(returnMaterialAutoStore);
                        }
                    }
                });
    }

    public void UpdateOutStorageListScanMaterialStatus(Context context, String out_storageList_id, String mStatus, String mid) {

        mModel.UpdateOutStorageListScanMaterialStatus(out_storageList_id, mStatus, mid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String returnMaterialAutoStore) {
                        String result = returnMaterialAutoStore.substring(75, returnMaterialAutoStore.toCharArray().length - 9);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "");
                        } else {
//                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                            mRootView.UpdateOutStorageListScanMaterialStatusSuccess(returnMaterialAutoStore);
                        }
                    }
                });
    }

    public void UpdateOutStorageListOutStatus(Context context, String out_storageList_id, String outStatus) {

        mModel.UpdateOutStorageListOutStatus(out_storageList_id, outStatus).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String returnMaterialAutoStore) {
                        String result = returnMaterialAutoStore.substring(75, returnMaterialAutoStore.toCharArray().length - 9);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "");
                        } else {
//                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                            mRootView.UpdateOutStorageListOutStatusSuccess(returnMaterialAutoStore);
                        }
                    }
                });
    }

    public void resetOutStorageListScanStatus(Context context, Map<String, String> params) {

        mModel.resetOutStorageListScanStatus(params).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String returnMaterialAutoStore) {
                        String result = returnMaterialAutoStore.substring(75, returnMaterialAutoStore.toCharArray().length - 9);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "");
                        } else {
//                            List<OutOfTheLibraryList> outOfTheLibraryLists = ArmsUtils.parseString2List(result, OutOfTheLibraryList.class);

                            mRootView.resetOutStorageListScanStatusSuccess(returnMaterialAutoStore);
                        }
                    }
                });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
